<?php
ob_start();
/*
	控件每次向此文件POST数据
	逻辑：
		1.更新数据库进度
		2.将文件块数据保存到服务器中。
	更新记录：
		2017-05-31 创建
		2017-06-01 增加目录信息 
*/
require '../../vendor/autoload.php';

use database\DBFile;
use database\DBFolder;
use model\FileInf;
use biz\up7_biz_event;
use biz\FolderScaner;
use utils\WebBase;
use utils\PathTool;
use sql\SqlExec;

$wb = new WebBase;

$id	   = $wb->queryString("id");
$uid   = $wb->queryString("uid");
$cbk   = $wb->queryString("callback");
$cover = $wb->reqInt("cover");
$pathRel = $wb->queryString("pathRel");
$pathRel = PathTool::url_decode($pathRel);
$ret  	= 0;

//参数为空
if ( strlen($id) > 0 )
{	
    //是根目录
    $isRootDir = $pathRel == "/";
    $folder = null;
    $fdExist = null;
    
    $se = new SqlExec();
    //子目录
    if(!$isRootDir)
    {        
        $folder = DBFolder::read($id);
        $fdExist = DBFolder::read2($folder->pathRel,$id);
        
        //存在相同目录=>直接使用已存在的目录信息=>删除旧目录
        if($fdExist != null) DBFolder::del($fdExist->id,$uid);
    }//根目录
    else
    {
        $folder = DBFile::read2($id);
        $fdExist = DBFile::read3($folder->pathRel,$id,$uid);
        
        //存在相同目录=>直接使用已存在的目录信息=>删除旧目录
        if($fdExist != null) DBFile::del($uid, $fdExist->id);
    }
    
    if($fdExist != null)
    {
        $folder->id = $fdExist->id;
        $folder->pid = $fdExist->pid;
        $folder->pidRoot = $fdExist->pidRoot;        
    }
	
	//根节点
	$root = new FileInf();
	$root->id = $folder->pidRoot;
	$root->uid = $folder->uid;
	//当前节点是根节点
	if( empty(trim($root->id))) $root->id = $folder->id;
	
	$fsd = new FolderScaner();
	//覆盖同路径项
	$fsd->m_cover = $fdExist!=null;	
	$fsd->save($folder);
	
	//上传完毕
	if(!$isRootDir) DBFolder::complete2($id,$uid);
	//根目录
	else DBFile::complete($id);	
	$ret = 1;
	
	//触发事件
	up7_biz_event::folder_post_complete($id);
}
echo "$cbk( $ret )";
header('Content-Length: ' . ob_get_length());
?>